package com.itheima.huikebackend.common;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

import java.util.Map;

/**
 * R
 *
 * @author liliudong
 * @version 1.0
 * @description 全局统一响应类
 * @date 2023/7/26 15:56
 */
@Getter
public class R<T> extends JSONObject {
    private final static String SUCCESS = "success";

    public R() {

    }

    public R(int code, String msg, T data) {
        put("code", code);
        put("msg", msg);
        put("data", data);
    }

    public R(int code, String msg, Map<String, Object> map) {
        super(map);
        put("code", code);
        put("msg", msg);
    }

    public R(int code, String msg) {
        put("code", code);
        put("msg", msg);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @param msg  msg
     * @param code code
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(T data, String msg, int code) {
        return new R<>(code, msg, data);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @param msg  msg
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(T data, String msg) {
        return success(data, msg, 0);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(T data) {
        return success(data, SUCCESS);
    }

    /**
     * 成功
     *
     * @return {@link R}<{@link ?}>
     */
    public static <T> R<T> success() {
        return new R<>();
    }

    /**
     * 成功
     *
     * @param map
     * @param msg
     * @param code
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(Map<String, Object> map, String msg, int code) {
        return new R<>(code, msg, map);
    }

    /**
     * 成功
     *
     * @param map
     * @param msg
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(Map<String, Object> map, String msg) {
        return new R<>(0, msg, map);
    }

    /**
     * 成功
     *
     * @param map
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> success(Map<String, Object> map) {
        return new R<>(200, SUCCESS, map);
    }

    /**
     * 成功
     *
     * @param code
     * @param msg
     * @return {@link R}<{@link T}>
     */
    public static <T> R<T> error(int code, String msg) {
        return new R<>(code, msg);
    }

    /**
     * 错误
     *
     * @param code 代码
     * @return {@link R}<{@link ?}>
     */
    public static R error(int code) {
        return error(code, "error");
    }

    /**
     * 错误
     *
     * @param msg msg
     * @return {@link R}<{@link ?}>
     */
    public static R error(String msg) {
        return error(1000, msg);
    }

    /**
     * 错误
     *
     * @return {@link R}<{@link ?}>
     */
    public static R error() {
        return error(1000);
    }
}
